package com.example.wto.viewpagertest01;

import android.database.Cursor;

import java.util.ArrayList;
import java.util.List;

public class UtilLessonSql implements IUtilSql
{
	private UtilSql utilSql=null;
	public UtilLessonSql(UtilSqlHelper sqlHelper)
	{
		utilSql=new UtilSql(sqlHelper);
	}
	    public void insertLesson(Lesson lesson)
	    {
	    	LessonBean bean=lesson.getBean();
	    	String college_id=bean.getCollegeID();
	    	String college_name=bean.getCollegeName();
	    	String s_id=bean.getS_id();
	    	String s_name=bean.getS_name();
	    	String lesson_id=bean.getLessonID();
	    	String lesson_name=bean.getLessonName();
	        String sql="insert into lesson_table (college_id,college_name,s_id,s_name,lesson_id,lesson_name) values(?,?,?,?,?,?)";
	        List<Object> params=new ArrayList<Object>();
	        params.add(college_id);
	        params.add(college_name);
	        params.add(s_id);
	        params.add(s_name);
	        params.add(lesson_id);
	        params.add(lesson_name);
	        
	        utilSql.update(sql,params);
	        List<DayOfWeek> weekList=lesson.getWeekList();

	        for(int i=0;i<weekList.size();i++)
	        {
	        	params.clear();
	        	params.add(college_id);
	 	        params.add(s_id);
	 	        params.add(lesson_id);
	            List<Course> courseList=weekList.get(i).getCourseList();
	            String sql_one="insert into week_lesson_table values(?,?,?,?,?,?,?,?,?,?,?)";
	            params.add(weekList.get(i).getWeekName());
	            params.add(i+"");
	            for(int j=0;j<courseList.size();j++)
	            {
	                params.add(courseList.get(j).toString());
	            }
	            utilSql.update(sql_one,params);

	        }
	        utilSql.close();
	    }
	    public Lesson selectLesson(LessonBean bean)
	    {
	        boolean flag=false;
	        String sql="select t.college_id,t.college_name,t.s_id,t.s_name,t.lesson_id,t.lesson_name,order_t,week_name,course_one,course_two,course_three,course_four,course_five,course_six from lesson_table t ,week_lesson_table w where t.college_id=w.college_id and t.s_id = w.s_id and t.lesson_id = w.lesson_id and t.college_id=? and t.s_id=? and t.lesson_id=? order by order_t;";
	        
	        Lesson lesson=new Lesson(bean);
	        Cursor resultSet=utilSql.select(sql,new String[]{bean.getCollegeID(),bean.getS_id(),bean.getLessonID()});
	        List<DayOfWeek> weekList=new ArrayList<DayOfWeek>();
	        try
			{
	            while(resultSet.moveToNext())
	            {
					List<Course> courses=new ArrayList<Course>();
					DayOfWeek weekday=new DayOfWeek();
					bean.setCollegeName(resultSet.getString(resultSet.getColumnIndex("college_name")));
					bean.setS_name(resultSet.getString(resultSet.getColumnIndex("s_name")));
					bean.setLessonName(resultSet.getString(resultSet.getColumnIndex("lesson_name")));
					//获取课程数据
					courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_one"))));
					courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_two"))));
					courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_three"))));
					courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_four"))));
					courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_five"))));
					courses.add(new Course(resultSet.getString(resultSet.getColumnIndex("course_six"))));
					//设置星期几
					weekday.setWeekName(resultSet.getString(resultSet.getColumnIndex("week_name")));
					//添加课程数据
					weekday.getCourseList().clear();
					weekday.getCourseList().addAll(courses);
					//添加星期数据
					weekList.add(weekday);
					flag=true;
	            }
	            lesson.setBean(bean);

			}
	        finally
	        {
				 resultSet.close();
	        	 utilSql.close();
	        }
	        if(flag)
	        {
	            lesson.getWeekList().clear();
	            lesson.getWeekList().addAll(weekList);
	            return lesson;
	        }
	        else
	            return null;
	    }
		@Override
		public void insert(Object obj)
		{
			Lesson lesson=(Lesson)obj;
			insertLesson(lesson);
		}
		@Override
		public Object select(Object obj)
		{
			LessonBean les=(LessonBean)obj;
			
			Lesson lesson=selectLesson(les);
			return lesson;
		}
}
